package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.TeensModeManage;
import com.qire.common.constant.Url;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.BR;
import com.qire.manhua.activity.BuyMembershipActivity;
import com.qire.manhua.databinding.ActivityBuyMembershipBinding;
import com.qire.manhua.dialog.PayWaySelectorOfBuyVipDialog;
import com.qire.manhua.dialog.TeensPasswordAuthorizeDialog;
import com.qire.manhua.dialog.TipsConfirmDialog;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.enumeration.PaymentMethod;
import com.qire.manhua.model.enumeration.TeensModeScene;
import com.qire.manhua.model.enumeration.VipPrivilegeType;
import com.qire.manhua.model.viewModel.impl.BuyMembershipViewModel;
import com.qire.manhua.model.viewModel.impl.BuyMembershipViewModel.MembersPackageItemViewModel;
import com.qire.manhua.model.viewModel.impl.BuyMembershipViewModel.MembersFreeComicItemViewModel;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class BuyMembershipController extends EventController<BuyMembershipActivity> implements AutoVariable {

    private BuyMembershipViewModel viewModel;

    public BuyMembershipController(@NonNull BuyMembershipActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityBuyMembershipBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setBuyMembershipController(this);
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.buyMembershipController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(BuyMembershipViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void backBtnOnClick(View view){
        owner.onBackPressed();
    }

    public void receiveBtnOnClick(View view){
        if(!UserEntity.self.isVip()){
            new TipsConfirmDialog(view.getContext())
                    .setDialogTitle("温馨提示")
                    .setDialogContent("您还不是会员哟~请先开通会员。")
                    .setDialogBtnText("我知道了")
                    .show();
            return;
        }
        viewModel.receiveDailyAward();
    }

    public void waresLayoutOnClick(View view){
        MembersPackageItemViewModel membersPackageItemViewModel = (MembersPackageItemViewModel) view.getTag();
        membersPackageItemViewModel.toggle();
    }

    public void protocolBtnOnClick(View view) {
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.WebPage)
                .withData("title", "付费会员服务协议")
                .withData("url", Url.buyMemberProtocol_url)
                .navigation();
    }

    public void payButtonOnClick(View view) {
        if(TeensModeManage.teensModeEnable()) {
            new TeensPasswordAuthorizeDialog(view.getContext(), TeensModeScene.Recharge, this::onPay).show();
            return;
        }
        onPay();
    }
    private void onPay() {
        if (!viewModel.haveChosen()) {
            ToastHelper.showSnackBar("请先选择购买套餐", Prompt.WARNING);
            return;
        }

        new PayWaySelectorOfBuyVipDialog(owner, v -> {
            PaymentMethod paymentMethod = ObjectUtil.as(v.getTag(), PaymentMethod.class);
            if(ObjectUtil.nonNull(paymentMethod)) {
                viewModel.buyBuyMembership(paymentMethod);
            }
        }).show();
    }

    /**
     * 找朋友代付单击事件
     */
    public void friendPaidBtnOnClick(View view) {
        FloorCard floorCard = AntsRouter.ROUTER.buildCard(Navigation.Page.FriendPaidPage);
        if(TeensModeManage.teensModeEnable()) {
            new TeensPasswordAuthorizeDialog(view.getContext(), TeensModeScene.Recharge, floorCard::navigation).show();
            return;
        }
        floorCard.navigation();
    }

    public void freeComicItemOnClick(View view){
        MembersFreeComicItemViewModel itemViewModel = (MembersFreeComicItemViewModel) view.getTag();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicReaderPage)
                .withData("comicBookEntity", new ComicBookEntity(itemViewModel.comicId, itemViewModel.title))
                .navigation();
    }

    public void vipInstructionsBtnOnClick(View view){
        VipPrivilegeType type = (VipPrivilegeType) view.getTag();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.VipInstructionsPage)
                .withData("type", type)
                .navigation();
    }

}
